//
//  VVComploseToolBar.swift
//  Weibo
//
//  Created by 王惠平 on 2017/4/6.
//  Copyright © 2017年 Will Wang. All rights reserved.
//

import UIKit

enum VVComposeToolBarButtonType: Int {
    
    //图片
    case picture = 0
    
    //@
    case mention = 1
    
    //#
    case trent = 2
    
    //表情
    case emoticon = 3
    
    //加号
    case add = 4
}

class VVComploseToolBar: UIStackView {
    
    //点击toolBar按钮闭包的回调
    var buttonActionClosure: ((VVComposeToolBarButtonType)->())?
    
    //表情按钮
    var emoticonButton: UIButton?
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        
        //水平方向布局
        self.axis = .horizontal
        
        //子控件等比填充
        self.distribution = .fillEqually
        
        addChildButton(imageName: "compose_camerabutton_background",type: .picture)
        addChildButton(imageName: "compose_mentionbutton_background", type: .mention)
        addChildButton(imageName: "compose_trendbutton_background", type: .trent)
        emoticonButton = addChildButton(imageName: "compose_emoticonbutton_background", type: .emoticon)
        addChildButton(imageName: "compose_add_background", type: .add)
    }
    
    //添加子控件
    @discardableResult //表示可以忽略结果
    fileprivate func addChildButton(imageName: String, type: VVComposeToolBarButtonType)-> UIButton {
        
        let button = UIButton()
        
        //设置tag
        //枚举的原始值作为tag使用表示(0,1,2,3,4就是你设置的原始值)
        button.tag = type.rawValue
        
        //添加点击事件
        button.addTarget(self, action: #selector(buttonAction(btn:)), for: .touchUpInside)
        
        //设置不同状态下的图片
        button.setImage((UIImage(named: imageName)), for: .normal)
        button.setImage(UIImage(named: "\(imageName)_highlighted"), for: .highlighted)
        button.setBackgroundImage(UIImage(named:"compose_toolbar_background"), for: .normal)
        
        //取消高亮
        button.adjustsImageWhenHighlighted = false
        
        //添加按钮
        addArrangedSubview(button)
        
        return button
    }

    //点击事件
    @objc private func buttonAction(btn: UIButton) {
        
        //通过枚举原始值获取枚举
        let buttonType = VVComposeToolBarButtonType(rawValue: btn.tag)!
        
        //点击按钮闭包的回调
        buttonActionClosure?(buttonType)
    }
    
    //根据是否是表情键盘类型切换对应的icon
    func switchEmoticonIcon(isEmoticon: Bool) {
        
        if isEmoticon {
            
            //表情的icon改成系统键盘
            emoticonButton?.setImage(UIImage(named:"compose_keyboardbutton_background"), for: .normal)
            emoticonButton?.setImage(UIImage(named:"compose_keyboardbutton_background_highlighted"), for: .selected)
            
        } else {
            
            //系统键盘改成表情的icon
            emoticonButton?.setImage(UIImage(named:"compose_emoticonbutton_background"), for: .normal)
            emoticonButton?.setImage(UIImage(named:"compose_emoticonbutton_background_highlighted"), for: .selected)
        }
        
    }
    
}
